package dao;
import pojo.*;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class UserSql {
    public static User Login(String name){
        String sql="select * from user where name='"+name+"'";
        ResultSet rs= JDBCconection.getData(sql);
        User user=null;
        try {
            while (rs.next()){
                user=new User();
                user.setName(rs.getString(2));
                user.setPwd(rs.getString(3));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return user;
    }
    //注册用户
    public static void save(User user){
        String sql="insert into user value(null,'"+user.getName()+"','"+user.getPwd()+"','"+user.getPhone()+"')";
        JDBCconection.updata(sql);
    }
    //查询用户
    public static List<User> queryUser(String name) {
        List<User> list=new ArrayList<>();
        String sql="select * from user where name='"+name+"'";
        ResultSet  rs= JDBCconection.getData(sql);
        User u=null;
        try {
            while(rs.next()){
                u=new User();
                u.setId(rs.getInt(1));
                u.setName(rs.getString(2));
                u.setPwd(rs.getString(3));
                u.setPhone(rs.getString(4));
                list.add(u);
            }
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return list;
    }

    public static List<User> getUserName(String u_name) {
        List<User> list=new ArrayList<>();
        String sql="select name,phone from user where name like '%"+u_name+"%'";
        ResultSet  rs= JDBCconection.getData(sql);
        User user=null;
        try {
            while(rs.next()){
                user=new User();
                user.setName(rs.getString(1));
                user.setPhone(rs.getString(2));
                list.add(user);
            }
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return list;
    }
    //更改用户信息
    public static Boolean updateUser(User u) {
        String sql="update user set name='"+u.getName()+"',phone='"+u.getPhone()+"' where id='"+u.getId()+"'";
        return JDBCconection.updata(sql);
    }
    //查询旧密码
    public static String getOldpwd(int id) {
        String pwd=null;
        String sql="select pwd from user where id='"+id+"'";
        ResultSet  rs= JDBCconection.getData(sql);
        try {
            while(rs.next()){
                pwd=rs.getString(1);
            }
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }


        return pwd;
    }
    public static boolean  updatePwd(int id,String pwd){
        String sql="update user set pwd='"+pwd+"' where id='"+id+"'";
        return JDBCconection.updata(sql);
    }
}
